﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

using CRM.Entity.Extra;
using CRM.Entity;
using CRM.Dictionary.Box;
using CRM.Entity.Page;


namespace CRM.Dictionary.Service
{
    public partial class News_Detail_DicService
    {
        public static ReaderWriterLockSlim News_Detail_rws = new ReaderWriterLockSlim();

        public News_DetailPage News_Detail_List()
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                IEnumerable<News_Detail> listEntity = DictionaryBox.News_DetailDictionary.Values;

                var News_DetailPage = new News_DetailPage()
                {
                    ListEntity = listEntity.ToList(),
                    RecordCount = listEntity.Count()
                };

                return News_DetailPage;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }

        public News_DetailPage News_Detail_List(PageInfo page)
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                IEnumerable<News_Detail> listEntity = DictionaryBox.News_DetailDictionary.Values;

                var News_DetailPage = new News_DetailPage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.news_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return News_DetailPage;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }
        
        public News_DetailPage News_Detail_List(Func<News_Detail, bool> func)
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                IEnumerable<News_Detail> listEntity = DictionaryBox.News_DetailDictionary.Values;

                var News_DetailPage = new News_DetailPage()
                {
                    ListEntity = listEntity.Where(func).ToList(),
                    RecordCount = listEntity.Count()
                };

                return News_DetailPage;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }

        public News_DetailPage News_Detail_List(PageInfo page, Func<News_Detail, bool> func)
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                IEnumerable<News_Detail> listEntity = DictionaryBox.News_DetailDictionary.Values
                                                                .Where(func);

                var News_DetailPage = new News_DetailPage()
                {
                    ListEntity = listEntity.AsParallel().OrderByDescending(i => i.news_id)
                                  .Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).ToList(),
                    RecordCount = listEntity.AsParallel().Count()
                };

                return News_DetailPage;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }

        public List<News_Detail> News_Detail_List_Top(int top, Func<News_Detail, bool> func)
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                IEnumerable<News_Detail> listEntity = DictionaryBox.News_DetailDictionary.Values.Where(func);

                return listEntity.AsParallel().OrderByDescending(i => i.news_id).Take(top).ToList();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }

        public News_Detail News_Detail_Multiple(Func<News_Detail, bool> func)
        {
            try
            {
                News_Detail_rws.EnterReadLock();

                return DictionaryBox.News_DetailDictionary.Values.Where(func).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitReadLock();
            }
        }

        public News_Detail News_Detail_Single(Int32 News_DetailID)
        {
            if(DictionaryBox.News_DetailDictionary.ContainsKey(News_DetailID))
                return DictionaryBox.News_DetailDictionary[News_DetailID];
            return null;
        }

        public bool News_Detail_Add(News_Detail News_Detail)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_DetailDictionary.TryAdd(News_Detail.news_id, News_Detail);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }
        
        public bool News_Detail_AddBatch(List<News_Detail> list)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var item in list)
                    isSuccess = DictionaryBox.News_DetailDictionary.TryAdd(item.news_id, item);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }

        public bool News_Detail_Update(News_Detail News_Detail)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_DetailDictionary.TryUpdate(News_Detail.news_id,
                                          News_Detail, DictionaryBox.News_DetailDictionary[News_Detail.news_id]);

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }
        
        public bool News_Detail_UpdateBatch(List<News_Detail> list)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                   isSuccess = true;

                foreach (var single in list)
                {
                    isSuccess = DictionaryBox.News_DetailDictionary.TryUpdate(single.news_id,
                                              single, DictionaryBox.News_DetailDictionary[single.news_id]);
                }

                return isSuccess;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }

        public bool News_Detail_Remove(News_Detail News_Detail)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                isSuccess = DictionaryBox.News_DetailDictionary.TryRemove(News_Detail.news_id, out News_Detail);

                return isSuccess;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }
        
        public bool News_Detail_RemoveList(List<Int64> list)
        {
            try
            {
                News_Detail_rws.EnterWriteLock();

                bool isSuccess = false;

                if (list.Count == 0)
                    isSuccess = true;

                foreach (var id in list)
                {
                    News_Detail single;

                    isSuccess = DictionaryBox.News_DetailDictionary.TryRemove(id, out single);
                }

                return isSuccess;
            }
            finally
            {
                News_Detail_rws.ExitWriteLock();
            }
        }
    }
}
